package com.spyz.p1_wirtable_1;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class FlowMapper extends Mapper<LongWritable, Text, Text, com.spyz.P4_Partitioner_Flow_1.FlowBean> {
    private Text outK = new Text();
    private FlowBean outV = new FlowBean();

    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, com.spyz.P4_Partitioner_Flow_1.FlowBean>.Context context) throws IOException, InterruptedException {

        // 1、获取一行数据
        //1,15799760342,167.33.255.42,https://www.weimao.org/,646,746,569
        String line = value.toString();
        System.out.println(line);
        // 2、切分
        //{1,15799760342,167.33.255.42,https://www.weimao.org/,646,746,569}
        // 0 1            2             3                       4  5   6
        String[] split = line.split(",");
        for (String s : split) {
            System.out.print(s+" ");
        }
        System.out.println();
        // 3、获取指定数据
        String phone = split[1];//手机号
        long upFlow = Long.parseLong(split[split.length-3]);//上行流量
        long downFlow = Long.parseLong(split[split.length-2]);//下行流量

        // 4、封装
        outK.set(phone);//手机号
        outV.setUpFlow(upFlow);//上行流量
        outV.setDownFlow(downFlow);//下行流量
        outV.setSumFlow();//总流量

        // 5、写出
        context.write(outK,outV);
    }
}
